import random

from selenium.webdriver.common.by import By

from base.base_action import BaseAction


class SettingPage(BaseAction):
    address_manage_button = By.XPATH, "//*[@text='地址管理']"
    address_button = By.XPATH, "//*[@text='新增地址']"
    consignee_edit_text = By.XPATH, "//*[@text='收件人姓名2–15个字符']"
    tel_edit_text = By.XPATH, "//*[@text='11位手机号']"
    detail_edit_text = By.XPATH, "//*[@text='5–60个字符']"
    postcode_edit_text = By.XPATH, "//*[@text='可选填']"
    default_radio = By.ID, "com.yunmall.lc:id/address_default"
    save_button = By.XPATH, "//*[@text='保存']"
    select_button = By.XPATH, "//*[@text='请选择']"
    address_info = By.ID, "com.yunmall.lc:id/receipt_name"

    def click_address_manage(self):
        self.click(self.address_manage_button)

    def click_address(self):
        self.click(self.address_button)

    def input_consignee(self, text):
        self.send_keys(self.consignee_edit_text, text)

    def input_tel(self, text):
        self.send_keys(self.tel_edit_text, text)

    def input_detail(self, text):
        self.send_keys(self.detail_edit_text, text)

    def input_postcode(self, text):
        self.send_keys(self.postcode_edit_text, text)

    def click_default_radio(self):
        self.click(self.default_radio)

    def click_save(self):
        self.click(self.save_button)

    def click_select(self):
        self.click(self.select_button)
    """方式一:"""
    # def random_click(self):
    #     while True:
    #         try:
    #             eles = self.driver.find_elements_by_id("com.yunmall.lc:id/area_title")
    #             # print(len(eles))
    #             num = random.randint(0, len(eles)-1)
    #             eles[num].click()
    #         except Exception:
    #             break
    """方式二:"""
    def random_click(self):
        while True:
            interface = self.driver.current_activity
            if interface == "com.yunmall.ymctoc.ui.activity.ProvinceActivity":
                eles = self.driver.find_elements_by_id("com.yunmall.lc:id/area_title")
                # print(len(eles))
                num = random.randint(0, len(eles)-1)
                eles[num].click()
            else:
                break

    def get_text_address_info(self):
        return self.get_text(self.address_info)


